﻿<Page
    x:Name="PageRoot"
    x:Class="MobileServicesExample.SubscribersPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:MobileServicesExample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    DataContext="{Binding ViewModel, RelativeSource={RelativeSource Self}}"
    d:DataContext="{Binding Source={d:DesignInstance local:SampleSubscribersViewModel, IsDesignTimeCreatable=True}}"
    >

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Page.Resources>
        <local:GenderToImagePathConverter x:Name="GenderToImagePathConverter"/>
    </Page.Resources>
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.ChildrenTransitions>
            <TransitionCollection>
                <EntranceThemeTransition/>
            </TransitionCollection>
        </Grid.ChildrenTransitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="PrimaryColumn" Width="420"/>
            <ColumnDefinition x:Name="SecondaryColumn" Width="*"/>
        </Grid.ColumnDefinitions>

        <!-- Back button and page title -->
        <Grid Grid.ColumnSpan="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <!--<Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                        Style="{StaticResource NavigationBackButtonNormalStyle}"
                        VerticalAlignment="Top"
                        AutomationProperties.Name="Back"
                        AutomationProperties.AutomationId="BackButton"
                        AutomationProperties.ItemType="Navigation Button"/>-->
            <TextBlock Text="Mobile Services Example" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1"
                        IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,0,40"/>
        </Grid>

        <!-- Vertical scrolling item list -->
        <ListView
            x:Name="ItemListView"
            AutomationProperties.AutomationId="ItemsListView"
            AutomationProperties.Name="Items"
            TabIndex="1"
            Grid.Row="1" Grid.Column="0"
            Margin="-10,-10,0,0"
            Padding="120,0,0,60"
            IsSwipeEnabled="False"
            ItemsSource="{Binding Subscribers}"
            SelectedItem="{Binding SelectedSubscriber, Mode=TwoWay}"
            SelectionMode="Single"
            SelectionChanged="ItemListView_SelectionChanged">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="6">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Border Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" Width="60" Height="60">
                            <Image Source="{Binding Gender, Converter={StaticResource GenderToImagePathConverter}}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Gender}"/>
                        </Border>
                        <StackPanel Grid.Column="1" Margin="10,0,0,0">
                            <TextBlock Text="{Binding LastName}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="NoWrap" MaxHeight="40"/>
                            <TextBlock Text="{Binding FirstName}" Style="{StaticResource CaptionTextBlockStyle}" TextWrapping="NoWrap"/>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemContainerStyle>
                <Style TargetType="FrameworkElement">
                    <Setter Property="Margin" Value="0,0,0,10"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>


        <!-- Details for selected item -->
        <ScrollViewer
            x:Name="ItemDetail"
            AutomationProperties.AutomationId="ItemDetailScrollViewer"
            Grid.Column="1"
            Grid.Row="1"
            Padding="60,0,66,0"
            DataContext="{Binding SelectedSubscriber}"
            HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"
            HorizontalScrollMode="Disabled" VerticalScrollMode="Enabled"
            ZoomMode="Disabled">

            <Grid x:Name="ItemDetailGrid" Margin="0,0,0,50">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Image Grid.Row="1" Margin="0,0,20,0" Width="180" Height="180" Source="{Binding Gender, Converter={StaticResource GenderToImagePathConverter}}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Gender}"/>
                <StackPanel Grid.Row="1" Grid.Column="1">
                    <TextBlock Margin="0,-10,0,0" Text="{Binding LastName}" Style="{StaticResource SubheaderTextBlockStyle}"/>
                    <TextBlock Margin="0,0,0,20" Text="{Binding FirstName}" Style="{StaticResource SubtitleTextBlockStyle}"/>
                </StackPanel>
                <TextBlock Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="0" Margin="0,10,0,0" Text="{Binding MailingAddress}" Style="{StaticResource BodyTextBlockStyle}"/>
                <TextBlock Grid.Row="3" Grid.ColumnSpan="2" Grid.Column="0" Margin="0,10,0,0" Text="{Binding Phone}" Style="{StaticResource BodyTextBlockStyle}"/>
            </Grid>
        </ScrollViewer>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ViewStates">
                <VisualState x:Name="PrimaryView" />
                <VisualState x:Name="SinglePane">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PrimaryColumn" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="*"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SecondaryColumn" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemDetail" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemListView" Storyboard.TargetProperty="Padding">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="120,0,90,60"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <!--
                    When an item is selected and only one pane is shown the details display requires more extensive changes:
                     * Hide the master list and the column it was in
                     * Move item details down a row to make room for the title
                     * Move the title directly above the details
                     * Adjust padding for details
                 -->
                <VisualState x:Name="SinglePane_Detail">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PrimaryColumn" Storyboard.TargetProperty="Width">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemListView" Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemDetail" Storyboard.TargetProperty="(Grid.Row)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemDetail" Storyboard.TargetProperty="(Grid.RowSpan)">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemDetailGrid" Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="0,0,0,60"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ItemDetail" Storyboard.TargetProperty="Padding">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="120,0,90,0"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
    <Page.BottomAppBar>
        <CommandBar>
            <AppBarButton Icon="Refresh" Label="Refresh" Command="{Binding RefreshItemsCommand}"/>
            <AppBarSeparator/>
            <AppBarButton Icon ="Add" Label="Add" Command="{Binding AddSubscriberCommand}">
                <AppBarButton.Flyout>
                    <Flyout>
                        <Grid Width="320" Margin="20,10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <local:SubscriberEditControl Grid.Row="0" DataContext="{Binding SubscriberBeingEdited}" d:DataContext="{Binding Path=SelectedSubscriber}"/>
                            <Grid Grid.Row="1">
                                <Button Content="Save" HorizontalAlignment="Center" Command="{Binding SaveSubscriberBeingAddedCommand}"/>
                            </Grid>
                        </Grid>
                    </Flyout>
                </AppBarButton.Flyout>
            </AppBarButton>
            <AppBarButton Icon="Edit" Label="Edit" Command="{Binding EditSelectedSubscriberCommand}">
                <AppBarButton.Flyout>
                    <Flyout>
                        <Grid Width="320" Margin="20,10">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <local:SubscriberEditControl Grid.Row="0" DataContext="{Binding SubscriberBeingEdited}" d:DataContext="{Binding Path=SelectedSubscriber}"/>
                            <Grid Grid.Row="1">
                                <Button Content="Save" HorizontalAlignment="Center" Command="{Binding SaveSubscriberBeingEditedCommand}"/>
                            </Grid>
                        </Grid>
                    </Flyout>
                </AppBarButton.Flyout>
            </AppBarButton>
            <AppBarButton Icon="Delete" Label="Delete" Command="{Binding DeleteSelectedSubscriberCommand}"/>
        </CommandBar>
    </Page.BottomAppBar>
</Page>
